from sqlalchemy import CHAR, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.extensions import Base, INT_PK, STR_NOTNULL_20

class Student(Base):
    __tablename__ = "students"
    id: Mapped[INT_PK]
    name: Mapped[STR_NOTNULL_20]
    gender: Mapped[str] = mapped_column(CHAR(1), nullable=False, default="男")
    clas_id: Mapped[int] = mapped_column(ForeignKey("clases.id"), nullable=False)
    # 关联属性，指定反向关系名studentList
    clas: Mapped["Clas"] = relationship(back_populates="studentList", lazy=False)
    def __repr__(self) -> str:
        return f"<Student id={self.id}, name='{self.name}', gender='{self.gender}', clas_id={self.clas_id}>"

if __name__ == '__main__':
    from app.extensions import engine
    from app.models.Clas import Clas

    Base.metadata.create_all(engine)
    print("学生表创建成功")